﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CampusMarket.Model;
using CampusMarket.ViewModel;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;

namespace CampusMarket
{
    /// <summary>
    /// WindowPostItem.xaml 的交互逻辑
    /// </summary>
    public partial class WindowPostItem : Window
    {
        public VMPostItem vm = new VMPostItem();
        private int status = 0;     //窗口状态 0-新增 1-修改
        public WindowPostItem()
        {
            this.DataContext = this.vm;
            this.vm.imgList = new ObservableCollection<string>();
            InitializeComponent();
        }
        public WindowPostItem(Goods some_item)
        {
            this.vm.goods = some_item;
            this.vm.imgList = new ObservableCollection<string>();
            this.status = 1;
            this.DataContext = this.vm;
            InitializeComponent(); 
            this.title.Content = "编辑商品";
            this.icon.Source = new BitmapImage(new Uri(@"img/appbar.edit.box.png", UriKind.Relative));
        }

        private void browseFile_Click(object sender, RoutedEventArgs e)
        {
            var fileDialog = new Microsoft.Win32.OpenFileDialog();
            fileDialog.FileName = "Image file";
            fileDialog.DefaultExt = "*.png; *.jpg; *.jpeg; *.bmp;*.tif; *.tiff; *.gif";
            fileDialog.Filter = "Image file|*.jpg; *.jpeg; *.bmp;*.tif; *.tiff; *.gif";
            var result = fileDialog.ShowDialog();
            if (true == result)
            {
                string filename = fileDialog.FileName;
                this.vm.curPath = filename;
                this.vm.imgList.Add(filename);
                // TODO: upload one of the images and get remote url to write
                //remoteUrl = imageUpload();
                //this.vm.goods.Goods_ImageUrl = remoteUrl;
            }
        }

        private void confirm_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.coworkingWnd = null; this.Close();
            switch (status)
            { 
                case 0:
                    addItem();break;
                case 1:
                    updateItem();break;
            }
        }

        private void tag_Click(object sender, RoutedEventArgs e)
        {
            ToggleButton tb = sender as ToggleButton;
            vm.goods.Goods_Type = tb.Content.ToString();
        }

        private void chooseMapPt_Click(object sender, RoutedEventArgs e)
        {
            switch (status) 
            {
                case 1:
                    if (Owner is WindowShelf)
                        Owner.Hide();
                    break;
            }
            MainWindow.mapControlStatus = 1;
            MainWindow.coworkingWnd = this;
            this.Hide();
        }
        private void addItem()
        {
            MessageBox.Show(vm.ToString());
            // TODO: Unwrap ViewModel and wrap as necessary, and add to database
        }
        private void updateItem()
        {
            MessageBox.Show("Update\n" + vm.ToString());
            // TODO: actually update database
        }
    }
}
